# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
  scipen = 999,
  digits = 16,
  max.print = .Machine$integer.max,
  show.error.locations = TRUE,
  warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------

library(data.table)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Just in case something masks 'source()', use extra deliberate base::source().
base::source("~/code/0-utility-functions/table-and-text-utils.R", local = TRUE)

# READ IN OUR DATA -------------------------------------------------------------

summary_dt <-
    readRDS(
        "~/estimation-output/winner_identity_regression_results.rds"
    )

# MAKE TABLE -------------------------------------------------------------------

filedir <- "~/paper/tables/table-A.8.tex"
file.create(filedir)

# Start Table A.8
start_latex_table(column_structure = "lccc", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "toprule", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

heading_text_input1 <-
    " &  & \\multicolumn{2}{c}{Identity in Household} "
write_latex_table_row(heading_text_input1, file_dir = filedir, append = TRUE)

add_latex_cmidrule(
    point_start_col = "l",
    point_end_col = "r",
    start_col = 3,
    end_col = 4,
    file_dir = filedir,
    append = TRUE
)

heading_text_input2 <-
     " \\multirow{2}{*}{Covariate} & \\multirow{2}{*}{Statistic} & Winner & Spouse "
write_latex_table_row(heading_text_input2, file_dir = filedir, append = TRUE)

heading_text_input3 <- "   &  & (1) & (2) "
write_latex_table_row(heading_text_input3, file_dir = filedir, append = TRUE)

heading_text_input1 <- NULL
heading_text_input2 <- NULL
heading_text_input3 <- NULL
rm(heading_text_input1, heading_text_input2, heading_text_input3)

add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

# Add body of Table A.8
wage_earnings_input <-
  sprintf(
    "Wage Earnings & Mean & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = summary_dt[winner == 1]$wages,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[winner == 0]$wages,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(wage_earnings_input, file_dir = filedir, append = TRUE)
wage_earnings_input <- NULL
rm(wage_earnings_input)

employment_input <-
  sprintf(
    "Employment & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[winner == 1]$employment,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[winner == 0]$employment,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(employment_input, file_dir = filedir, append = TRUE)
employment_input <- NULL
rm(employment_input)

age_input <-
  sprintf(
    "Age & Mean & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[winner == 1]$age,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[winner == 0]$age,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(age_input, file_dir = filedir, append = TRUE)
age_input <- NULL
rm(age_input)

gender_input <-
  sprintf(
    "Female & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[winner == 1]$female,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[winner == 0]$female,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(gender_input, file_dir = filedir, append = TRUE)
gender_input <- NULL
rm(gender_input)

primary_earner_input <-
  sprintf(
    "Primary Earner & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[winner == 1]$primary_earner,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[winner == 0]$primary_earner,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(primary_earner_input, file_dir = filedir, append = TRUE)
primary_earner_input <- NULL
rm(primary_earner_input)

older_member_input <-
  sprintf(
    "Older Member & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[winner == 1]$older_member,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[winner == 0]$older_member,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(older_member_input, file_dir = filedir, append = TRUE)
older_member_input <- NULL
rm(older_member_input)

same_age_input <-
  sprintf(
    "Same Age & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[winner == 1]$same_age,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[winner == 0]$same_age,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(same_age_input, file_dir = filedir, append = TRUE)
same_age_input <- NULL
rm(same_age_input)

add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

# taken from printed summary in /19-test-random-assignment-within-hh.R
p_value_input <-
    " &  & \\multicolumn{2}{c}{$p < 0.001$} "
write_latex_table_row(p_value_input, file_dir = filedir, append = TRUE)
p_value_input <- NULL
rm(p_value_input)

# End Table A.8
add_latex_table_rule(
  rule_type = "bottomrule",
  file_dir = filedir,
  append = TRUE
)
end_latex_table(file_dir = filedir, append = TRUE)
filedir <- NULL
rm(filedir)

# Housekeeping
summary_dt <- NULL
rm(summary_dt)